# -*- coding: utf-8 -*-
# AUTHOR SHIZHONG
import pandas as pd


def percentile_cal(window, percentile, data):
    """
    Percentile Calculation.
    """
    if len(data) == 0:
        return [0]
    percentile_data = []
    window = len(data) if window > len(data) else window
    for index in xrange(window - 1, len(data)):
        temp = sorted(data[index - window + 1:index])
        if (window * percentile / 100.0).is_integer() is True:
            percentile_data.append(
                (
                    temp[window * percentile / 100 - 1] +
                    temp[window * percentile / 100]
                ) / 2
            )
        else:
            percentile_data.append(temp[int(window * percentile / 100.0) - 1])
    return percentile_data


def rsi_cal(window, percentile_list, data_location):
    """
    RSI Parameters Calculation.
    """
    data_read = pd.read_csv(data_location)
    rsi_data = data_read.ix[0, 1:]

    rsi_parm = []
    for percentile in percentile_list:
        rsi_parm.append(percentile_cal(window, percentile, rsi_data))

    rsi = pd.DataFrame(rsi_parm)
    rsi.to_excel('rsi.xlsx', sheet_name='sheet1')


rsi_cal(
    22, [10, 30, 60, 90],
    data_location='C:\Users\Alexandre\Desktop\monthForCal.csv'
)
